package cn.darkhorse.device.service.impl;

import cn.darkhorse.common.constant.CommonConstant;
import cn.darkhorse.common.constant.WarnTypeEnum;
import cn.darkhorse.device.mapper.MesMsgWarningMapper;
import cn.darkhorse.device.pojo.*;
import cn.darkhorse.device.service.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 业务提醒 服务实现类
 * </p>
 *
 * @author yuan jun
 * @since 2023-02-03
 */
@Service
public class MesMsgWarningServiceImpl extends ServiceImpl<MesMsgWarningMapper, MesMsgWarning> implements MesMsgWarningService {
  /*  @Resource
    private MesDeviceIdleRecordsService mesDeviceIdleRecordsService;
    @Resource
    private MesSparePartsService mesSparePartsService;*/
//    @Resource
//    private MesSpotCheckPlanService mesSpotCheckPlanService;
//    @Resource
//    private MesMaintenancePlanService mesMaintenancePlanService;
//    @Resource
//    private MesRepairInfoService mesRepairInfoService;
//    @Resource
//    private MesSpotCheckDetailService mesSpotCheckDetailService;
//    @Resource
//    private MesMaintenanceDetailService mesMaintenanceDetailService;
    /**
     * 列表
     *
     * @param deviceName
     * @param beginTime
     * @param endTime
     * @param userId
     * @param readFlag
     * @param msgType
     * @return
     */
    @Override
    public List<MesMsgWarning> listAll(String deviceName, String beginTime, String endTime, Long userId, Integer msgType, Integer readFlag) {
        if(null==userId){
            throw new ServiceException("缺失用户信息！");
        }
        if(null==msgType){
            throw new ServiceException("缺失分类信息！");
        }
        LambdaQueryWrapper<MesMsgWarning> wrapper=new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(deviceName)){
            wrapper.like(MesMsgWarning::getDeviceMsg,deviceName);
        }
        if(StringUtils.isNotBlank(endTime)) {
            wrapper.le(MesMsgWarning::getCreateTime,endTime);
        }
        if(StringUtils.isNotBlank(beginTime)) {
            wrapper.ge(MesMsgWarning::getCreateTime,beginTime);
        }
        wrapper.eq(MesMsgWarning::getMsgType,msgType);
        wrapper.eq(MesMsgWarning::getReceiverId,userId);
        wrapper.eq(MesMsgWarning::getReadFlag,readFlag);
        wrapper.orderByAsc(MesMsgWarning::getCreateTime);
        return list(wrapper);
    }

    /**
     * 数量
     *
     * @param deviceName
     * @param beginTime
     * @param endTime
     * @param userId
     * @param readFlag
     * @param msgType
     * @return
     */
    @Override
    public Long count(String deviceName, String beginTime, String endTime, Long userId, Integer msgType, Integer readFlag) {
        if(null==userId){
            throw new ServiceException("缺失用户信息！");
        }
        LambdaQueryWrapper<MesMsgWarning> wrapper=new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(deviceName)){
            wrapper.like(MesMsgWarning::getDeviceMsg,deviceName);
        }
        if(StringUtils.isNotBlank(endTime)) {
            wrapper.le(MesMsgWarning::getCreateTime,endTime);
        }
        if(StringUtils.isNotBlank(beginTime)) {
            wrapper.ge(MesMsgWarning::getCreateTime,beginTime);
        }
        if(null!=msgType){
            wrapper.eq(MesMsgWarning::getMsgType,msgType);
        }
        wrapper.eq(MesMsgWarning::getReceiverId,userId);
        wrapper.eq(MesMsgWarning::getReadFlag,readFlag);
        List<MesMsgWarning> list = list(wrapper);
        return (long) list.size();
    }

    /**
     * 更新为已读
     *
     * @param id
     * @return
     */
    @Override
    public boolean read(Long id) {
        MesMsgWarning warning=getById(id);
        if(null!=warning){
            warning.setReadTime(LocalDateTime.now());
            LambdaQueryWrapper<MesMsgWarning> wrapper =new LambdaQueryWrapper<>();
            wrapper.eq(MesMsgWarning::getMsgType,warning.getMsgType());
            wrapper.eq(MesMsgWarning::getBusinessId,warning.getBusinessId());
            wrapper.eq(MesMsgWarning::getReadFlag, CommonConstant.MSG_UNREAD);
            //闲置
            if(WarnTypeEnum.IDEl.getType()==warning.getMsgType()){
               /* MesDeviceIdleRecords idleRecords=mesDeviceIdleRecordsService.getById(warning.getBusinessId());
                warning.setReadFlag(CommonConstant.MSG_READ);
                updateById(warning);
                if(null==idleRecords){
                    throw new ServiceException("单据已删除！");
                }*/
            }
            //备件库管理
            if(WarnTypeEnum.SPARE_PARTS.getType()==warning.getMsgType()){
             /*   MesSpareParts spareParts=mesSparePartsService.getById(warning.getBusinessId());
                warning.setReadFlag(CommonConstant.MSG_READ);
                updateById(warning);
                if(null==spareParts){
                    throw new ServiceException("单据已删除！");
                }*/
            }
//            //点检
//            if(WarnTypeEnum.SPOT_CHECK.getType()==warning.getMsgType()){
//                wrapper.like(MesMsgWarning::getBusinessMsg,warning.getBusinessMsg().substring(warning.getBusinessMsg().length()-3,warning.getBusinessMsg().length()));
//                //update(entry,wrapper);
//                warning.setReadFlag(CommonConstant.MSG_READ);
//                updateById(warning);
//                if(warning.getBusinessMsg().contains("点检异常")){
//                    MesSpotCheckDetail detail=mesSpotCheckDetailService.getById(warning.getBusinessId());
//                    if(null==detail){
//                        throw new ServiceException("单据已删除！");
//                    }
//                }else{
//                    MesSpotCheckPlan plan=mesSpotCheckPlanService.getById(warning.getBusinessId());
//                    if(null==plan){
//                        throw new ServiceException("单据已删除！");
//                    }
//                }
//            }
//            //保养
//            if(WarnTypeEnum.MAINTENANCE.getType()==warning.getMsgType()){
//                wrapper.like(MesMsgWarning::getBusinessMsg,warning.getBusinessMsg().substring(warning.getBusinessMsg().length()-3,warning.getBusinessMsg().length()));
//                //update(entry,wrapper);
//                warning.setReadFlag(CommonConstant.MSG_READ);
//                updateById(warning);
//                if(warning.getBusinessMsg().contains("保养异常")){
//                    MesMaintenanceDetail detail=mesMaintenanceDetailService.getById(warning.getBusinessId());
//                    if(null==detail){
//                        throw new ServiceException("单据已删除！");
//                    }
//                }else{
//                    MesMaintenancePlan plan=mesMaintenancePlanService.getById(warning.getBusinessId());
//                    if(null==plan){
//                        throw new ServiceException("单据已删除！");
//                    }
//                }
//            }
//            //维修
//            if(WarnTypeEnum.REPAIRE.getType()==warning.getMsgType()){
//                MesRepairInfo repairInfo=mesRepairInfoService.getById(warning.getBusinessId());
//                wrapper.like(MesMsgWarning::getBusinessMsg,warning.getBusinessMsg().split("-")[2]);
//                //update(entry,wrapper);
//                warning.setReadFlag(CommonConstant.MSG_READ);
//                updateById(warning);
//                if(null==repairInfo){
//                    throw new ServiceException("单据已删除！");
//                }
//            }
        }else{
            throw new ServiceException("无对应消息！");
        }
        return false;
    }
}
